package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;
import com.dl698.axdr.Unsigned16;
import com.dl698.axdr.Unsigned8;

import java.io.IOException;

public class OMD implements AxdrType {

    private byte[] dataCode = null;
    private Unsigned16 oi = null;
    private Unsigned8 mi = null;
    private Unsigned8 opm = null;

    public OMD() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setOmd(Unsigned16 oi, Unsigned8 mi, Unsigned8 opm) {
        this.oi = oi;
        this.mi = mi;
        this.opm = opm;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;

        this.oi = new Unsigned16();
        this.oi.setConst();
        try {
            codeLength += this.oi.decode(input);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        this.mi = new Unsigned8();
        this.mi.setConst();
        codeLength += this.mi.decode(input);

        this.opm = new Unsigned8();
        this.opm.setConst();
        codeLength += this.opm.decode(input);

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        int codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(this.dataCode[i]);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            if (this.oi != null && this.mi != null && this.opm != null) {
                codeLength = 0;
                try {
                    codeLength += this.opm.encode(output);
                    codeLength += this.mi.encode(output);
                    codeLength += this.oi.encode(output);
                } catch (RuntimeException e) {
                    throw e;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            } else {
                throw new RuntimeException("oi || mi || opm is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.oi != null && this.mi != null && this.opm != null) {
            return "sequence: {" + "oi: " + this.oi + ", mi: " + this.mi + ", opm: " + this.opm + "}";
        } else {
            return "oi || mi || opm is null";
        }
    }

    public Unsigned16 getOi() {
        return oi;
    }

    public Unsigned8 getMi() {
        return mi;
    }

    public Unsigned8 getOpm() {
        return opm;
    }

    /**
     * 获取数据编码
     * 
     * @return 数据编码
     */
    public byte[] getDataCode() {
        return dataCode;
    }
}